package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.shopping.converter.CartItemConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: bwj
 * @Date: 2022/09/20/19:55
 * @Description:
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    RedissonClient redissonClient;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    CartItemConverter cartItemConverter;


    /**
    * @Author: 宾文杰 @Description: 在redis购物车中更新商品信息状态 @Date: 2022/9/20
    */
    @Override
    public Integer newCart(CartRequest newCartsBo) {
        RMap<Long, CartProductDto> map = redissonClient.getMap(newCartsBo.getUserId().toString());
        CartProductDto cartDto = map.get(newCartsBo.getProductId());
        if (cartDto == null) {
            //如果取不到东西
            return -1;
        }
        cartDto.setProductNum(newCartsBo.getProductNum());
        cartDto.setChecked(newCartsBo.getChecked());
        map.put(newCartsBo.getProductId(), cartDto);
        return 0;
    }

    /**
    * @Author: 宾文杰 @Description: 删除购物车中的商品 @Date: 2022/9/20
    */
    @Override
    public void deleteCart(Integer uid,Integer pid) {
        RMap<Long, CartProductDto> map = redissonClient.getMap(uid.toString());
        map.remove(pid.longValue());
    }

    /**
    * @Author: 宾文杰 @Description: 添加商品到购物车 @Date: 2022/9/20
    */
    @Override
    public void putCart(CartRequest cartRequest) {
        RMap<Long, CartProductDto> map = redissonClient.getMap(cartRequest.getUserId().toString());
        Item item = itemMapper.selectByPrimaryKey(cartRequest.getProductId());
//        System.out.println(item);
//        Item item = itemMapper.selectItemById(cartRequest.getProductId());
        //取第一个照片
        String[] images = item.getImages();
        item.setImageBig(images[0]);
        CartProductDto cartProductDto = cartItemConverter.item2Dto(item);
        //刚放进去都为选中状态，有数量
        cartProductDto.setProductNum(cartRequest.getProductNum());
        cartProductDto.setChecked("true");
        map.put(cartRequest.getProductId(),cartProductDto);
    }

    /**
    * @Author: 宾文杰 @Description: 删除购物车中的全部商品 @Date: 2022/9/20
    */
    @Override
    public void deleteItems(Long uid) {
        RMap<Object, CartProductDto> map = redissonClient.getMap(uid.toString());
        Set<Object> objects = map.keySet();
        for (Object object : objects) {
            CartProductDto cartProductDto = map.get(object);
            if (cartProductDto.getChecked().equalsIgnoreCase("true")) {
                map.remove(object);
            }
        }
    }

    /**
    * @Author: 宾文杰 @Description: 更新全选购物车中的商品状态 @Date: 2022/9/20
    */
    @Override
    public void newItems(CartRequest cartRequest) {
        RMap<Long, CartProductDto> map = redissonClient.getMap(cartRequest.getUserId().toString());
        Set<Long> pids = map.keySet();
        for (Long pid : pids) {
            CartProductDto cartProductDto = map.get(pid);
            cartProductDto.setChecked(cartRequest.getChecked());
            map.put(pid, cartProductDto);
        }
    }

    /**
    * @Author: 宾文杰 @Description: 获得购物车列表 @Date: 2022/9/20
    */
    @Override
    public List<CartProductDto> carts(Long uid) {

        RMap<Long, CartProductDto> map = redissonClient.getMap(uid.toString());
        if (map == null) {
            //返回null代表购物车为空
            return null;
        }
        LinkedList<CartProductDto> cartProductDtos = new LinkedList<>();
        for (Map.Entry<Long, CartProductDto> entry : map.entrySet()) {
            cartProductDtos.add(entry.getValue());
        }
        return cartProductDtos;
    }
}
